// apps/h5/src/stores/useBaseData.ts
import { defineStore } from "pinia"
import { ref } from "vue"
import { getCmConfigList } from "../api" // 根据实际目录调整路径
import type BeanCmConfig from "../bean/cmConfig/BeanCmConfig" // 同上
import type BeanCmConfigRequest from "../bean/cmConfig/BeanCmConfigRequest" // 同上

export const useCmConfigStore = defineStore("cmConfig", () => {
  // 定义 Store 状态
  const cmConfigList = ref<BeanCmConfig[] | null>(null)
  const ready = ref<boolean>(false)
  const merchantCompanyCode = ref<string>("")

  // 调用接口获取数据
  async function fetchCmConfigList(params: BeanCmConfigRequest) {
    // 调用封装的 API 方法
    const { data, fetchData } = getCmConfigList(params)

    // 调用接口请求（实际请求触发）
    await fetchData()

    // 将接口返回结果同步到 Store 状态
    cmConfigList.value = data.value ? data.value.data : null
    merchantCompanyCode.value = cmConfigList.value
      ? cmConfigList.value[0].merchantCompanyCode
      : ""
    ready.value = true
  }

  return { cmConfigList, ready, merchantCompanyCode, fetchCmConfigList }
})
